﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using 掌控校园.ViewModels;

namespace 掌控校园
{
    public partial class CoursePageSettings : PhoneApplicationPage
    {
        private static BackgroundImageList viewModel = null;
        public static BackgroundImageList ViewModel
        {
            get
            {
                if (viewModel != null)//如果已经初始化那么就直接返回
                    return viewModel;
                string dataFromAppSetting;
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(App.IsoImageList, out dataFromAppSetting))//尝试从IsoStore中获取信息
                {
                    viewModel = JsonConvert.DeserializeObject<BackgroundImageList>(dataFromAppSetting);
                    return viewModel;
                }
                viewModel = new BackgroundImageList();//如果没有在IsoStore中获取到信息，那么直接载入本地的数据——也就是应用第一次开启时会被执行
                viewModel.LoadData();//应用第一次开启时会被执行
                return viewModel;

            }
        }

        public CoursePageSettings()
        {
            InitializeComponent();

            //LoadDataFromIsoStore();

            DataContext = ViewModel;
        }

        //private void LoadDataFromIsoStore()
        //{
        //    string dataFromAppSetting;
        //    if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(App.IsoImageList, out dataFromAppSetting))
        //    {
        //        viewModel = JsonConvert.DeserializeObject<BackgroundImageList>(dataFromAppSetting);
        //    }
        //}


        #region ToggleSwitch Checked && UnChecked
        private void Push_Checked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["WhetherPush"] = true;
            App.WhetherPush = true;
        }

        private void Push_UnChecked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["WhetherPush"] = false;
            App.WhetherPush = false;

        }

        private void DynamicTile_Checked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["DynamicTile"] = true;
            App.DynamicTile = true;
        }

        private void DynamicTile_UnChecked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["DynamicTile"] = false;
            App.DynamicTile = false;
        }

        private void ChangeColor_Checked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["WhetherChangeColor"] = true;
            App.WhetherChangeColor = true;
        }

        private void ChangeColor_UnChecked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["WhetherChangeColor"] = false;
            App.WhetherChangeColor = false;
        }

        private void ChangeBackground_Checked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["WhetherChangeBackground"] = true;
            App.WhetherChangeBackground = true;
            BackgroundLongListSelector.Visibility = Visibility.Collapsed;
        }

        private void ChangeBackground_UnChecked(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["WhetherChangeBackground"] = false;
            App.WhetherChangeBackground = false;
            BackgroundLongListSelector.Visibility = Visibility.Visible;
        }
        #endregion

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            PushToggleSwitch.IsChecked = App.WhetherPush;
            TileToggleSwitch.IsChecked = App.DynamicTile;
            ChangeColorToggleSwitch.IsChecked = App.WhetherChangeColor;
            ChangeBackgroundoggleSwitch.IsChecked = App.WhetherChangeBackground;

            if (App.WhetherChangeBackground) 
                BackgroundLongListSelector.Visibility = Visibility.Collapsed;

        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            var data = JsonConvert.SerializeObject(viewModel);
            IsolatedStorageSettings.ApplicationSettings[App.IsoImageList] = data;

            IsolatedStorageSettings.ApplicationSettings.Save();//保存信息
        }

        private void ChangeBackgroundUri_SelectionChangd(object sender, SelectionChangedEventArgs e)
        {
            LongListSelector selector = sender as LongListSelector;
            if (selector == null) 
                return;
            BackgroundImage img = (BackgroundImage) selector.SelectedItem;
            App.BackgroundImageUri = img.FilePath;
            IsolatedStorageSettings.ApplicationSettings["BackgroundImageUri"] = App.BackgroundImageUri;

        }

    }

    public class PathToImage : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                BitmapImage img = new BitmapImage();
                string filePath = (string)value;
                using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (iso.FileExists(filePath))
                    {
                        using (IsolatedStorageFileStream stream = iso.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                        {
                            img.SetSource(stream);
                        }
                    }
                    else
                    {
                        img = null;
                    }
                }

                return img;
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
}